package m.framework.web.view;

import java.util.ArrayList;
import java.util.List;

import m.framework.dao.mapping.BeanInfo;
import m.framework.dao.model.ModelHolder;
import m.framework.utils.StrBuilder;
import m.framework.utils.StringUtil;
import m.permission.model.BaseInfo;
import net.sf.cglib.proxy.Callback;
import net.sf.cglib.proxy.Enhancer;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


/**在页面创建select框*/
public class Select {
	private static Logger log = LoggerFactory.getLogger(Select.class);
	
	private static Select select;
	static{
		Enhancer en = new Enhancer();
		en.setSuperclass(Select.class);
		Callback cb =new TagInterceptor() ;
		en.setCallback(cb);
		select = (Select) en.create();
	}
	public static Select getInstance() {
		return select;
	} 
	
	public String baseInfo(String name,String type,String first,String attributs){
		Sel s = new Sel(name,attributs);
		List<Option> ops=new ArrayList();
		List<BaseInfo> bs=BaseInfo.get(type);
		if (StringUtil.isNotEmpty(first)) {
			ops.add(new Option("", first));
		}
		if(bs!=null){
			for(BaseInfo b:bs){
				Option op=new Option(b.getKey(), b.getValue());
				ops.add(op);
			}
		}
		
		return createSelect(s, ops);
	}
	
	public String enums(String name,String first,String attributs) throws Exception{
		return enums(name, first,null,attributs);
	}
	
	/**
	 * name的格式为model.field,例如性别user.gender
	 * 最终生成select的name为field
	 * */
	public String enums(String name,String first,Enum select,String attributs) throws Exception{
		String[] str=name.split("\\.");
		Class clz=ModelHolder.get(str[0]);
		log.debug("准备创建枚举:{}",clz+"."+str[1]);
		Class enumClazz=BeanInfo.getBeanInfo(clz).getField(str[1]).getReturnType();
		return enums(str[1],enumClazz,first,select,attributs);
	}
	
	public String enums(String name,Class enumClass, String first, Enum selected, String attributs) throws Exception {
		Sel s = new Sel(name,attributs);
		List<Option> ops = new ArrayList();
		if (StringUtil.isNotEmpty(first)) {
			ops.add(new Option("", first));
		}
		Enum[] ems = (Enum[]) enumClass.getMethod("values").invoke(enumClass);
		for (Enum em : ems) {
			String text = em.toString();
			Option op=new Option(em.ordinal() + "", text);
			if(em==selected){
				op.isSelect=true;
			}
			ops.add(op);
		}
		return createSelect(s, ops);
	}
	
	private String createSelect(Sel sel, List<Option> ops) {
		StrBuilder sb = new StrBuilder();
		sb.append("<select name='" + sel.name +sel.attributs+ "'>");
		for (Option op : ops) {
			sb.append("<option value='" + op.value + "' " + op.select() + ">" + op.text + "</option>");
		}
		sb.append("</select>");
		return sb.toString();
	}
	
	private class Sel {
		String name = "";
		String attributs="";
		public Sel(String name,String attributs) {
			this.name = name;
			if(attributs!=null){
				this.attributs=attributs;
			}
		}
	}
	
	private class Option {
		String value;
		String text;
		boolean isSelect = false;

		public Option(String value, String text) {
			this.value = value;
			this.text = text;
		}
		
		public String select(){
			if(isSelect){
				return "selected";
			}
			return "";
		}
	}
	
}
